//
//  ViewController.m
//  List
//
//  Created by Hunk on 12-2-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#include <mach/mach.h> 

@implementation ViewController

- (void)dealloc
{
    [listView release];
        
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"didReceiveMemoryWarning" 
                                                        message:[NSString stringWithFormat:@"%.4fM", [self availableMemory]]
                                                       delegate:nil
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

    testArray = [[NSMutableArray alloc] init];
    [testArray addObjectsFromArray:[NSArray arrayWithObjects:@"0", @"1", @"2", @"3", @"4", nil]];

    
    listView = [[UIListView alloc] initWithFrame:self.view.bounds];
    [listView appendDataArray:testArray];
    [listView setHasMore:YES];
    [self.view addSubview:listView];
    

//    label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
//    [label setTag:111];
//    [label setBackgroundColor:[UIColor blackColor]];
//    [label setText:nil];
//    [label setFont:[UIFont systemFontOfSize:14.0]];
//    [label setTextColor:[UIColor whiteColor]];
//    [self.view addSubview:label];

    
/*
    timer = [NSTimer timerWithTimeInterval:1.0/60.f target:self 
                                  selector:@selector(timerHandle:) 
                                  userInfo:nil 
                                   repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    
    
    memoryTimer = [NSTimer timerWithTimeInterval:1.0f target:self 
                                        selector:@selector(memoryTimerHandle:) 
                                        userInfo:nil 
                                         repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:memoryTimer forMode:NSDefaultRunLoopMode];
*/
}

- (void)timerHandle:(id)sender
{   
    NSInteger lastNumber = [[testArray lastObject] intValue];
    
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    
    for(int i = 1; i <= 5; i++)
    {
        [tempArray addObject:[NSString stringWithFormat:@"%d", (lastNumber + i)]];
    }

    [listView appendDataArray:tempArray];
    [tempArray release];
    
    [listView scrollListView];
}

-(double)availableMemory
{
    vm_statistics_data_t vmStats;
    
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    
    kern_return_t kernReturn = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
    
    if(kernReturn != KERN_SUCCESS) 
    {
        return NSNotFound;
    }
    return (double)((vm_page_size * vmStats.free_count) / 1024.0) / 1024.0;
}

- (void)memoryTimerHandle:(id)sender
{
    [label setText:[NSString stringWithFormat:@"%.4fM", [self availableMemory]]];
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
